﻿using AnesSystem.Controls;
using AnesSystem.Models;
using AnesSystem.Util;
using Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Threading;

namespace AnesSystem.ViewModels
{
    public class PacuInfusionCtrlViewModel : DrugBaseViewModel
    {
        #region Variables

        private ObservableCollection<PatientDrugsModel> _drugList;

        private ObservableCollection<FrameworkElement> _druglineList;

        private ObservableCollection<string> _totalDrugList;

        private DispatcherTimer timer;
        #endregion

        #region Constructor

        public PacuInfusionCtrlViewModel(int page)
        {
            CurPage = page;
            _drugList = new ObservableCollection<PatientDrugsModel>();
            _totalDrugList = new ObservableCollection<string>();
            _druglineList = new ObservableCollection<FrameworkElement>();
            InitalTimer();
        }



        #endregion

        #region Properties
        public ObservableCollection<PatientDrugsModel> DrugList
        {
            get
            {
                return _drugList;
            }

            set
            {
                _drugList = value;
            }
        }

        public ObservableCollection<FrameworkElement> DruglineList
        {
            get
            {
                return _druglineList;
            }

            set
            {
                _druglineList = value;
            }
        }

        public ObservableCollection<string> TotalDrugList
        {
            get
            {
                return _totalDrugList;
            }

            set
            {
                _totalDrugList = value;
            }
        }
        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void InitalTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 1, 0);
            timer.Tick += Timer_Tick; ;
            timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            CalDrugsLine();
            timer.Start();
        }

        private void InitialDrugLines()
        {
            DruglineList.Clear();
            DrugsDataCache drugDataCache = OperationDataPool.Instance.CurOperationData.DrugsDataCache;
            bool flag = DataManager.Instance.CurrentPatientLink.Status.OutPacuTime != new DateTime();
            foreach (var items in drugDataCache.PacuInfusionMapCodeList)
            {
                List<PatientDrugsModel> pList = new List<PatientDrugsModel>();
                foreach (var item in items.Value)
                {
                    if (item.Single)
                    {
                        if (item.BeginTime.CompareTo(GetPacuPageStartTime()) >= 0 && item.BeginTime.CompareTo(GetPacuPageEndTime()) <= 0)
                            pList.Add(item);
                    }
                    else
                    {
                        if (item.BeginTime.CompareTo(GetPageEndTime()) > 0) continue;
                        if (item.EndTime != new DateTime() && item.EndTime.CompareTo(GetPacuPageStartTime()) < 0) continue;
                        pList.Add(item);
                    }
                }
                if (pList.Count > 0)
                {
                    DrugLineContainerCtrl lineContainer = new DrugLineContainerCtrl();
                    lineContainer.CurrentPage = CurPage;
                    lineContainer.DataContext = pList;
                    lineContainer.DrawLinePacuContainer();
                    DruglineList.Add(lineContainer);
                }
            }
        }

        private void CalDrugsLine()
        {
            foreach (DrugLineContainerCtrl row in DruglineList)
            {
                foreach (DrugLineBoxCtrl lineBox in row.DrugLineBoxList)
                {
                    foreach (var lineItem in lineBox.DrugLineItemMap)
                    {
                        if (DataManager.Instance.CurrentPatientLink != null && DataManager.Instance.CurrentPatientLink.Status.OutPacuTime == new DateTime() && "working".Equals(lineItem.Key.Actions))
                        {
                            lineItem.Value.CurrentLine.CalPacuDrugLineX2();
                        }
                    }
                }
            }
        }
        private void InitialDrugs()
        {
            DrugList.Clear();
            TotalDrugList.Clear();
            DrugsDataCache drugDataCache = OperationDataPool.Instance.CurOperationData.DrugsDataCache;
            bool flag = DataManager.Instance.CurrentPatientLink.Status.OutPacuTime != new DateTime();
            foreach (var items in drugDataCache.PacuInfusionMapCodeList)
            {
                double total = 0;
                string totals = "";
                string unit = AnesRecordUtil.Instance.GetJiLiangUnit(items.Value[0]);
                for (int i = 0; i < items.Value.Count; i++)
                {
                    total += AnesRecordUtil.Instance.CalTotalAmount(items.Value[i]);
                    if (items.Value[i].Single)
                    {
                        if (items.Value[i].BeginTime.CompareTo(GetPacuPageStartTime()) >= 0 && items.Value[i].BeginTime.CompareTo(GetPacuPageEndTime()) <= 0)
                        {
                            List<PatientDrugsModel> plist = DrugList.ToList().FindAll(delegate (PatientDrugsModel p) { return p.Drugs.HashCode == items.Value[i].Drugs.HashCode; });//返回所有匹配
                            if (plist == null || plist.Count < 1) DrugList.Add(items.Value[i]);
                            if (i == items.Value.Count() - 1)
                            {
                                if (HospitalConfig.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_SC_CD_TYFSYY))
                                {
                                    TotalDrugList.Add("");
                                }
                                else
                                {
                                    totals = unit != null ? ("总量:" + total + unit) : "";
                                    TotalDrugList.Add(totals);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (items.Value[i].BeginTime.CompareTo(GetPacuPageEndTime()) > 0) continue;
                        if (items.Value[i].EndTime != new DateTime() && items.Value[i].EndTime.CompareTo(GetPageStartTime()) < 0) continue;
                        List<PatientDrugsModel> plist = DrugList.ToList().FindAll(delegate (PatientDrugsModel p) { return p.Drugs.HashCode == items.Value[i].Drugs.HashCode; });//返回所有匹配
                        if (plist == null || plist.Count < 1) DrugList.Add(items.Value[i]);

                        //病人出室
                        if (flag)
                        {
                            if (items.Value[i].EndTime != new DateTime() && items.Value[i].EndTime.CompareTo(GetPacuPageEndTime()) < 0)
                            {
                                totals = unit != null ? ("总量:" + total + unit) : "";
                            }
                            else
                            {
                                if (DataManager.Instance.CurrentPatientLink.Status.OutPacuTime.CompareTo(GetPacuPageEndTime()) < 0)
                                {
                                    totals = unit != null ? ("总量:" + total + unit) : "";
                                }
                            }
                        }
                        //病人未出室
                        else
                        {
                            if (items.Value[i].EndTime != new DateTime() && items.Value[i].EndTime.CompareTo(GetPacuPageEndTime()) < 0)
                            {
                                totals = unit != null ? ("总量:" + total + unit) : "";
                            }
                        }
                        if (i == items.Value.Count() - 1)
                        {
                            if (HospitalConfig.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_SC_CD_TYFSYY))
                            {
                                TotalDrugList.Add("");
                            }
                            else
                            {
                                TotalDrugList.Add(totals);
                            }
                        }
                    }
                }
            }
        }


        #endregion

        #region Public/Protected Methods

        #endregion
        public override void InitData()
        {
            //CalTotalAmount();
            InitialDrugs();
            InitialDrugLines();

        }

        public override void Refresh()
        {
            InitData();
        }


        #region EventHandlers

        #endregion
    }
}
